using System;
using System.Collections.Generic;
using System.Text;
using Colman.Rpg.Messaging;

namespace Colman.Rpg.Server.Workflow
{
    /// <summary>
    /// Handles the CONTROL messages
    /// </summary>
    public static class PlayerControlWorkflowHandler
    {
        /// <summary>
        /// Create Control response
        /// </summary>
        /// <param name="e"></param>
        public static void CreateResponses(WorkflowHandlerArgs e)
        {
            Player player = (e.Request.UserName == null ? null : PlayersCache.Get(e.Request.UserName));

            if (e.Request is PlayerPointsAdditionRequest)
            {
                if (player.AbilityPointsOwed > 0)
                {
                    PlayerPointsAdditionRequest pointsRequest = (PlayerPointsAdditionRequest)e.Request;
                    int ap = pointsRequest.AP, dp = pointsRequest.DP;

                    while (player.AbilityPointsOwed > 0 &&
                        (ap > 0 || dp > 0))
                    {
                        if (ap > 0)
                        {
                            player.AP++;
                            ap--;
                        }
                        else
                        {
                            player.DP++;
                            dp--;
                        }

                        player.AbilityPointsOwed--;
                    }

                    PlayersCache.Set(player);
                }
            }
            else if (e.Request is EnterShopRequest)
            {
                e.Responses.Add(new EnterShopResponse(Dal.GetAllItems(), player));
            }
            else if (e.Request is BuyItemRequest)
            {
                PlayersCache.Set(Dal.PlayerBuysItem(player, ((BuyItemRequest)e.Request).Item.Name));

                e.Responses.Add(new ItemTransactionResponse(PlayersCache.Get(e.Request.UserName)));
            }
            else if (e.Request is SellItemRequest)
            {
                PlayersCache.Set(Dal.PlayerSellsItem(player, ((SellItemRequest)e.Request).Item.Name));

                e.Responses.Add(new ItemTransactionResponse(PlayersCache.Get(e.Request.UserName)));
            }
            else if (e.Request is EquipArmorRequest)
            {
                PlayersCache.Set(Dal.EquipArmor(player, ((EquipArmorRequest)e.Request).Item));

                e.Responses.Add(new ItemTransactionResponse(PlayersCache.Get(e.Request.UserName)));
            }
            else if (e.Request is EquipWeaponRequest)
            {
                PlayersCache.Set(Dal.EquipWeapon(player, ((EquipWeaponRequest)e.Request).Item));

                e.Responses.Add(new ItemTransactionResponse(PlayersCache.Get(e.Request.UserName)));
            }
            else if (e.Request is PlayerInformationRequest)
            {
                e.Responses.Add(new PlayerInformationResponse(Dal.GetPlayer(e.Request.UserName)));
            }
        }
    }
}
